﻿using System;

using OpenQA.Selenium;

namespace MVCBootstrap.Tests.WebUI {

	public static class IWebDriverExtensions {

		public static void VisitURL(this IWebDriver driver, String relativeURL) {
			driver.Navigate().GoToUrl(String.Format("{0}{1}", TestConfiguration.SiteBaseURL, relativeURL));
		}

		public static void InsertText(this IWebDriver driver, String name, String text) {
			IWebElement box = driver.FindElement(By.Name(name));
			if (box == null) {
				throw new ApplicationException("No element with that name found");
			}
			driver.InsertText(box, text);
		}

		public static void InsertText(this IWebDriver driver, IWebElement element, String text) {
			element.Clear();
			element.SendKeys(text);
			//try {
			//    IJavaScriptExecutor js = driver as IJavaScriptExecutor;
			//    js.ExecuteScript("jQuery('#" + element.GetAttribute("id") + "').change();");
			//}
			//catch { }
		}

		public static IWebElement FindButton(this IWebDriver driver, String text) {
			return driver.FindElement(By.XPath(String.Format("//input[@type=\"submit\" and @value=\"{0}\"]", text)));
		}
	}
}